package cn.com.backstage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_region;
import cn.com.api.service.ApiT_regionService;
import cn.com.api.service.ApiUploadService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE + "common/")
public class CommonController {

	private static Logger logger = Logger.getLogger(CommonController.class);
	@Autowired
	private ApiT_regionService apiT_regionService;
	@Autowired
	private ApiUploadService apiUploadService;

	/**
	 * 上传图片到qiniu服务器，返回图片的地址
	 * 
	 * @param file
	 *            图片流集合
	 * @param foldername
	 *            图片存放的文件夹名称
	 * @throws Exception
	 */
	@RequestMapping(value = "uploadPicToQiNiu")
	public void uploadPicToQiNiu(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "file") MultipartFile[] files,
			String foldername) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<String> urlList = apiUploadService.uploadFileToQiNiu(files);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作成功");
			map.put(Utils.DATAS, urlList);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "上传失败！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 上传图片本地，返回图片的地址
	 * 
	 * @param file
	 *            图片流集合
	 * @param foldername
	 *            图片存放的文件夹名称
	 * @throws Exception
	 */
	@RequestMapping(value = "uploadPicToServer")
	public void uploadPicToServer(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "file") MultipartFile[] files,
			String foldername) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<String> urlList = apiUploadService.uploadFileToServer(files,
					request, "/member/");
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作成功");
			map.put(Utils.DATAS, urlList);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "上传失败！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获取省市区json格式数据接口
	 * 
	 * @param parent_id
	 *            父节点id(传1时，返回的是全国的省)
	 * @throws Exception
	 */
	@RequestMapping(value = "getAllProvincesRegions")
	public void getAllProvincesRegions(HttpServletRequest request,
			HttpServletResponse response, String parent_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			if (parent_id != null && !"".equals(parent_id)) {
				map.put("parent_id", parent_id);
			}
			List<ApiT_region> list = apiT_regionService.findT_regionByMap(map);
			map.clear();
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作成功");
			map.put(Utils.DATAS, list);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "上传失败！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 跳转到百度地图界面
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "baiduditu")
	public ModelAndView baiduditu(HttpServletRequest request,
			HttpServletResponse response, String parent_id) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/baiduditu.jsp");
		return mv;
	}

}
